package com.gimi.cloud.jimi.manage.service.prod.impl;


import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.MyBeanUtils;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.dao.dao.product.LogisticsCompanyDao;
import com.gimi.cloud.jimi.dto.product.LogisticsCompanyDTO;
import com.gimi.cloud.jimi.entity.product.LogisticsCompanyEntity;
import com.gimi.cloud.jimi.manage.service.prod.LogisticsCompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class LogisticsCompanyServiceImpl implements LogisticsCompanyService {
	
	private static Logger logger = LoggerFactory.getLogger(LogisticsCompanyServiceImpl.class);
	@Autowired
	private LogisticsCompanyDao logisticsCompanyDao;

	@Override
	public PageInfo<LogisticsCompanyEntity> logisticsCompanyPageList(LogisticsCompanyDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
        Example example = new Example(LogisticsCompanyEntity.class);
        Example.Criteria criteria = example.createCriteria();
        
        criteria.andCondition("is_delete =", "0");
        //公司名称模糊匹配查询
        if(!StringUtil.isBlank(dto.getCompanyName())){
        	criteria.andCondition("company_name like", "%"+dto.getCompanyName()+"%");
        }
        //公司地址模糊匹配
        if(!StringUtil.isBlank(dto.getCompanyAddress())){
        	criteria.andCondition("company_address like ", "%"+dto.getCompanyAddress()+"%");
        }
        List<LogisticsCompanyEntity> list = logisticsCompanyDao.selectByExample(example);
        PageInfo<LogisticsCompanyEntity> page = new PageInfo<>(list);
        return page;
	}

	@Override
	public LogisticsCompanyDTO getSingleLogisticsCompany(LogisticsCompanyDTO dto) {
		LogisticsCompanyEntity entity = logisticsCompanyDao.selectByPrimaryKey(dto.getLcId());
		LogisticsCompanyDTO LogisticsDTO = new LogisticsCompanyDTO();
		MyBeanUtils.copyProperties(entity,LogisticsDTO);
		return LogisticsDTO;
	}

	@Override
	public void updateLogisticsCompany(LogisticsCompanyDTO dto) {
		if (dto.getLcId() != null) {
			//更新
			LogisticsCompanyEntity entity = logisticsCompanyDao.selectByPrimaryKey(dto.getLcId());
			MyBeanUtils.copyPropertiesIgnoreNull(dto,entity);
	        entity.setUpdateTime(new Date());
	        int i = logisticsCompanyDao.updateByPrimaryKey(entity);
	        if (i <= 0 ) {
	        	throw new MsgException(ResponseCodeEnum.FAILURE, "更新物流公司信息失败!");
	        }
		}else {
			//添加
			LogisticsCompanyEntity entity = logisticsCompanyDao.selectByPrimaryKey(dto.getLcId());
			if (entity == null) {
				LogisticsCompanyEntity record = new LogisticsCompanyEntity();
				MyBeanUtils.copyPropertiesIgnoreNull(dto,record);
				record.setVersion(0);
				int i = logisticsCompanyDao.insert(record);
				if (i <= 0 ) {
					throw new MsgException(ResponseCodeEnum.FAILURE, "添加物流公司信息失败!");
				}
			}
		}
		
	}

	@Override
	public void deleteLogisticsCompany(LogisticsCompanyDTO dto) {
		LogisticsCompanyEntity entity = logisticsCompanyDao.selectByPrimaryKey(dto.getLcId());
		if (entity != null) {
            //删除订单
            entity.setIsDelete(1);//设置删除属性
            int i = logisticsCompanyDao.updateByPrimaryKey(entity);
            if (i <= 0 ) {
				throw new MsgException(ResponseCodeEnum.FAILURE, "删除失败!");
			}
        }
	}

}
